﻿using Dapper.DBContext.Schema;
using Guoc.BigMall.Infrastructure;
using Guoc.BigMall.Infrastructure.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Guoc.BigMall.Domain.Entity
{

    /// <summary>
    ///  用户信息，来自其他平台的登陆用户 
    /// </summary>
   public class Customer:BaseEntity
    {
        public Customer()
        {
            this.CreatedOn = DateTime.Now;
            this.UpdaptedOn = DateTime.Now;
            this.Status = CustomerStatus.Active;
            this.Password = "123456";
            this.InitEncryptionPassword();
        }

        /// <summary>
        ///  登陆账号 ：有手机号，存手机号
        /// </summary>
        public string UserName { get; set; }

        public string Password { get; set; }
        public string OpenId { get; set; }

        public string UnionId { get; set; }
        public string NickName { get; set; }

        public string RealName { get; set; }

        public string HeadImage { get; set; }

        public CustomerStatus Status { get; set; }
        /// <summary>
        /// 微信公众号，小程序，其他平台等
        /// </summary>
        public Platform Platform { get; set; }

        public DateTime CreatedOn { get; set; }

        public DateTime UpdaptedOn { get; set; }

        public int Sex { get; set; }

        /// <summary>
        ///  会员ID ，默认0 表示没有关联会员
        /// </summary>
        public int MemberId { get; set; }


        /// <summary>
        /// 加密密码
        /// </summary>
        public void EncryptionPassword()
        {
            MD5 md5Prider = MD5.Create();
            this.Password = md5Prider.GetMd5Hash(this.Password);
        }
        /// <summary>
        /// 初始化密码时，前后台一致
        /// </summary>
        public void InitEncryptionPassword()
        {
            MD5 md5Prider = MD5.Create();
            this.Password = md5Prider.GetMd5Hash(this.Password);
            // this.WebPassword = this.Password;           
        }

        public void CheckPassword(string password)
        {
            MD5 md5Prider = MD5.Create();
            if (!md5Prider.VerifyMd5Hash(password, this.Password))
            {
                throw new FriendlyException("账户或密码不正确!");
            }

        }

        public void CheckAccountState()
        {
            if (this.Status != CustomerStatus.Active) { throw new FriendlyException("账户未激活"); }
        }

    }

    public enum Platform { 
        [Description("微信公众号")]
        WechatMp =1 ,
        [Description("微信小程序")]
        WechatMini 
    }

    public enum CustomerStatus {

        [Description("激活")]
        /// <summary>
        /// 激活
        /// </summary>
        Active = 1,
        [Description("禁用")]
        /// <summary>
        /// 禁用
        /// </summary>
        Disabled = 2,
    }
}
